package com.fll.fangyitask.service.impl;

import com.fll.fangyicommon.entity.task.Record;
import com.fll.fangyicommon.utils.TimeUtil;
import com.fll.fangyicommon.vo.RecordVO;
import com.fll.fangyitask.dao.RecordDao;
import com.fll.fangyitask.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author fll
 * create 2023/4/4
 */
@Service
@Slf4j
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordDao recordDao;

    /**
     * 获取用户该学习和已学习的task数
     * @param userId
     * @return
     */
    @Override
    public List<RecordVO> getRecord(Integer userId) {
        List<RecordVO> recordVOList = new ArrayList<>(8);

        List<Record> recordList = recordDao.getRecord(userId);
        /**
         * 以下某天已学习的task数
         */
        int todayNum = 0; //今天已学习
        int yesterdayNum = 0; //昨天已学习
        int beforeYesterdayNum = 0; //前天已学习
        int threeDaysAgoNum = 0; //3天前已学习
        int fourDaysAgoNum = 0; //4天前已学习
        int fiveDaysAgoNum = 0; //5天前已学习
        int sixDaysAgoNum = 0; //6天前已学习
        int sevenDaysAgoNum = 0; //7天前已学习
        for (Record record : recordList){
            int day = TimeUtil.betweenDays(record.getRecordTime(), TimeUtil.getString(new Date()));
            switch (day){
                case 0:
                    todayNum ++;
                    break;
                case 1:
                    yesterdayNum ++;
                    break;
                case 2:
                    beforeYesterdayNum ++;
                    break;
                case 3:
                    threeDaysAgoNum ++;
                    break;
                case 4:
                    fourDaysAgoNum ++;
                    break;
                case 5:
                    fiveDaysAgoNum ++;
                    break;
                case 6:
                    sixDaysAgoNum ++;
                    break;
                case 7:
                    sevenDaysAgoNum ++;
                    break;
                default:
                    break;
            }
        }

        /**
         * 以下某天该学习的task总数
         */
        int todaySum = this.getSum(userId, 0); //今天该学习的总数
        int yesterdaySum = this.getSum(userId, 1); //昨天该学习的总数
        int beforeYesterdaySum = this.getSum(userId, 2); //前天该学习的总数
        int threeDaysAgoSum = this.getSum(userId, 3); //3天前该学习的总数
        int fourDaysAgoSum = this.getSum(userId, 4); //4天前该学习的总数
        int fiveDaysAgoSum = this.getSum(userId, 5); //5天前该学习的总数
        int sixDaysAgoSum = this.getSum(userId, 6); //6天前该学习的总数
        int sevenDaysAgoSum = this.getSum(userId, 7); //7天前该学习的总数

        recordVOList.add(new RecordVO(0,todayNum,todaySum));
        recordVOList.add(new RecordVO(1,yesterdayNum,yesterdaySum));
        recordVOList.add(new RecordVO(2,beforeYesterdayNum,beforeYesterdaySum));
        recordVOList.add(new RecordVO(3,threeDaysAgoNum,threeDaysAgoSum));
        recordVOList.add(new RecordVO(4,fourDaysAgoNum,fourDaysAgoSum));
        recordVOList.add(new RecordVO(5,fiveDaysAgoNum,fiveDaysAgoSum));
        recordVOList.add(new RecordVO(6,sixDaysAgoNum,sixDaysAgoSum));
        recordVOList.add(new RecordVO(7,sevenDaysAgoNum,sevenDaysAgoSum));
        return recordVOList;
    }

    /**
     * 添加学习记录
     * @param record
     * @return
     */
    @Override
    public Integer addRecord(Record record){
        return recordDao.addRecord(record);
    }

    /**
     * 获取用户某个特点日期学习的task数
     * @param userId
     * @param day
     * @return
     */
    @Override
    public int getSum(Integer userId, int day){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //加一天
        String dateString = df.format(new Date().getTime() + 1 * 24 * 3600 * 1000);
        String date = TimeUtil.dateStringSub(dateString, day);
        return recordDao.getSum(userId, date);
    }
}
